05. Instantiate Some Ships
Prepare
For this exercise, we are going to focus on writing the code responsible for placing our ships. So you’ll want to navigate to the ControlCenter.swift file and then to this area in the code where it says addShipsAndMines.
You'll write code for adding ships and mines to the grid in "addShipsAndMines".
Any code written inside the curly braces, {}, (where it says WRITE YOUR CODE HERE!) will be run before the grid is visible to the user (i.e. adding ships and mines). For this exercise, you will write code here that creates instances of the Ship struct and places them on the grid. If you recall, we learned that we can create instances of structs using an initializer.
The Ship struct's initializer can be used to create ships.
Let's start by using let since all our Ship properties are constant. Then, let's give this Ship a name… how about mediumShip1 since I want this Ship to be a medium size. We can use the equals sign and begin typing a Ship initializer. As soon as I type an open parentheses, Xcode autocompletes most of the default initilaizer.
In the initializer, one can specify the properties of a ship.
We can accept the suggestion by hitting tab and this will cause the cursor to move to the first value which is length. I’ll type 3, then hit tab again to go to the next value. For the location, we have to create a GridLocation struct, and we can do that right here inside the initializer. So I’ll type GridLocation and a parentheses, and then Xcode starts to auto-complete this initializer as well.
let mediumShip1 = Ship(length: 3, location: GridLocation(x:0, y:0),
isVertical: false)
I’ll make x and y both 0. Then, let’s set isVertical to false. Now we have our Ship instance, and we need to add it to the grid. To do this we will use a special line of code called a method or function.
class ControlCenter {
func addShipsAndMines(human: Human) {
let mediumShip1 = Ship(length: 3, location: GridLocation(x:0, y:0),
isVertical: false)
human.addShipToGrid(mediumShip1)
}
}
The code above is responsible for taking our Ship and placing it on the grid. And that’s it. Let’s hit play and see what happens.
The Debug Area provides information about why the game cannot start.
We can see our first ship, but we get an alert saying we do not have the correct amount of ships and to check the Debug Area for more details. It looks like we haven't added all the required ships yet!
Learn
That's where you come in. Now it is your turn to add the remaining ships to the grid. Add one more medium-sized ship, a small ship, a large ship, and an extra large ship. In total, the grid should have the following ships…
- 1 small ship (size 2)
- 2 medium ships (size 3)
- 1 large ship (size 4)
- 1 extra large ship (size 5)
As you add ships, keep in mind that a ship cannot overlap another ship, a ship must be in bounds, a ship cannot have a size less than 2 or exceeding 5, and you cannot add more ships than the grid expects for a certain size. If you violate any of these rules, then the ship will not be added to the grid and you'll get an error message. Check the Debug Area to see which rule isn't being satisfied:
Here, the Debug Area reports that a ship of length 0 cannot be added to the grid.
An Example Violation
Here's an example of adding more medium ships than the grid expects:
```swift
class ControlCenter {
func addShipsAndMines(human: Human) {
let mediumShip1 = Ship(length: 3, location: GridLocation(x: 0, y: 0), isVertical: false)
let mediumShip2 = Ship(length: 3, location: GridLocation(x: 0, y: 1), isVertical: false)
let mediumShip3 = Ship(length: 3, location: GridLocation(x: 0, y: 2), isVertical: false)
human.addShipToGrid(mediumShip1)
human.addShipToGrid(mediumShip2)
human.addShipToGrid(mediumShip3)
}
}```
This code attempts to add 3 medium ships when there should only be 2. So pay close attention as you add the remaining ships!
Once you’ve added all 4 ships, hit play and take on the computer for yourself!
If you run into technical issues, let our coaches know on the forums.